package cn.kuang.service;

import cn.kuang.mapper.ArticleContentMapper;
import cn.kuang.mapper.ArticleUserMapper;
import cn.kuang.mapper.UserMapper;
import cn.kuang.pojo.ArticleContent;
import cn.kuang.pojo.ArticleUser;
import cn.kuang.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 徽州大都督
 * @date 2020/7/25
 */
@Service
@Transactional
public class ArricleContentService {

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleUserMapper articleUserMapper;

    //private final static List<ArticleContent> contentList = null;

    public int addContent(ArticleContent articleContent) {

        //评论内容id
        String cid = UUID.randomUUID ().toString ();

        //文章id
        String tid = articleContent.getTid ();

        articleContent.setCid (cid);
        articleContent.setContentTime (new Date ());

        //添加articleContent表
        int row = articleContentMapper.insert (articleContent);


        //查询用户id
        User user = userMapper.selectOne (new QueryWrapper<User> ().eq ("username", articleContent.getUsername ()));
        String uid = user.getUid ();

        //将查询到的用户设置到articleContent
        articleContent.setUser (user);

        //添加ArticleUser表
        ArticleUser articleUser = new ArticleUser ();
        articleUser.setCid (cid);
        articleUser.setTid (tid);
        articleUser.setUid (uid);
        articleUserMapper.insert (articleUser);


        return row;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public List<ArticleContent> findPage(Integer page, Integer size, String tid) {

        QueryWrapper<ArticleUser> wrapper = new QueryWrapper<> ();
        wrapper.eq ("tid",tid);


        List<ArticleUser> articleUsers = articleUserMapper.selectList (wrapper);

        //articleUsers.forEach (System.out::println);
        Page<ArticleContent> page1 = new Page<> (page,size);
        QueryWrapper<ArticleContent> wrapper1 = new QueryWrapper<> ();

        List<String> cids = new ArrayList<> ();
        articleUsers.forEach (use->{
            String cid = use.getCid ();
            cids.add (cid);
        });



        //String sql="select cid from article_user where tid = ";



        //String sql = "'select cid from article_user where tid  "+tid;
        wrapper1.inSql ("cid", "select cid from article_user where tid = '"+ tid+"'");


        wrapper1.orderByDesc ("content_time");
        //wrapper1.in ()


        articleContentMapper.selectPage (page1, wrapper1);

        List<ArticleContent> records = page1.getRecords ();

        long total = page1.getTotal ();
        records.forEach (ss->{
            //System.out.println (ss.getUserContents ());
            ArticleUser articleUser = articleUserMapper.selectById (ss.getCid ());
            User user = userMapper.selectById (articleUser.getUid ());
            ss.setUser (user);
            ss.setTotal (total);
        });

        return records;
    }



}
